const request = require('../../utils/https.js')
var userId, orderId;
var imgAddCount = 0; //点击选择图片次数

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageFile: [], // 存储微信返回的图片地址
    imageData: [], // 存储后台返回的图片地址

    resultDArr: [], //后台返回的图片带顺序码集合
    hideAddBtn: false, // 是否显示上传图片的按钮,
    uploadBtns: true //上传按钮的显示与隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    userId = wx.getStorageSync('userId');
    orderId = options.subsId
  },
  bindTextAreaBlur(e) {
    const that = this;
    that.setData({
      value: e.detail.value
    })

  },
  onType(e) {
    const that = this;
    that.setData({
      type: e.target.dataset.type // 0 换货 1退
    })
  },
  /**
   * 图片的选择  上传
   */
  choosePic() {
    imgAddCount += 10;
    let that = this;
    let imageLength = that.data.imageFile.length;

    let imageFile = that.data.imageFile;
    let length = imageFile.length; // 多图上传，分几次上传时，改变状态问题，记录下上一次图片的长度
    let imageData = that.data.imageData;
    let resultDArr = that.data.resultDArr;
    // 显示上传按钮是，就能上传
    // if (!hideAddBtn){


    //图片选择
    wx.chooseImage({
      count: 3, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          uploadBtns: false, //控制视频图片上传按钮
          index: 0, //控制是视频显示还是图片显示
        })

        tempFilePaths.forEach((v, i) => {
          if (imageFile.length < 3) {
            imageFile.push({
              imgUrl: v,
              text: '上传中...'
            })

            // 图片上传
            wx.uploadFile({
              url: getApp().Api_url+'order/upFile',
              filePath: v,
              name: 'file',
              header: {
                'content-type': 'multipart/form-data'
              },
              success(response) {
                let resultD = JSON.parse(response.data);
                if (resultD.errMsg == 'OK') {
                  console.log('上传成功')
                  imageFile[length + i].text = '已上传';
                  resultDArr.push(resultD)
                  imageData.push(resultD.data)
                  that.setData({
                    imageFile: imageFile,
                    imageData: imageData,
                    resultDArr: resultDArr
                  })
                }
              },
              fail(res) {
                console.log(res)
              }
            })



          }
        })

        // 是否展示上传按钮
        if (imageFile.length > 2) {
          that.setData({
            hideAddBtn: true
          })
        } else {
          that.setData({
            hideAddBtn: false
          })
        }
        // 图片展示
        that.setData({
          imageFile: imageFile
        })
      }
    })
    // }
  },
  /**
   * 删除图片
   */
  deleteImg(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let imageFile = that.data.imageFile;
    let imageData = that.data.imageData;
    let resultDArr = that.data.resultDArr;
    // 删除第几张
    imageFile.splice(index, 1);
    imageData.splice(index, 1);
    resultDArr.splice(index, 1)
    that.setData({
      imageFile: imageFile,
      imageData: imageData,
      resultDArr: resultDArr
    })
    // 是否展示上传按钮
    if (imageFile.length > 2) {
      that.setData({
        hideAddBtn: true,

      })
    } else if (imageFile.length == 0) {
      that.setData({
        hideAddBtn: false,
        uploadBtns: true,
        index: 0,
      })

    } else {
      that.setData({
        hideAddBtn: false
      })
    }
  },
  postSubmit() {
    const that = this;
    let imageData = that.data.imageData;
    let type = that.data.type;
    let value = that.data.value;
    
    if (type == undefined) {
      wx.showToast({
        icon: 'none',
        title: '请选择原因!',
      })
      return;
    }
    if (value == undefined) {
      wx.showToast({
        icon: 'none',
        title: '请输入原因!',
      })
      return;
    }
    if (imageData.length <= 0) {
      wx.showToast({
        icon: 'none',
        title: '请上传图片!',
      })
      return;
    }

    request._post(getApp().Api_url + 'order/exchange', {
      /* userId: userId, */
      orderId: orderId,
      reason: value,
      pics: imageData,
      what: type,
    }, function(res) {
      if (res.data.errCode == 20000) {
        wx.navigateBack({
          delta: 2
        })

      }
    }, function() {})




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})